package com.thoughtservice.jspdoc.processors;


import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thoughtservice.jspdoc.JSPFileList;
import com.thoughtservice.jspdoc.JSPPage;
import com.thoughtservice.jspdoc.RE;
import com.thoughtservice.jspdoc.RESyntaxException;
import com.thoughtservice.jspdoc.utils.ProcessorUtils;
import com.thoughtservice.jspdoc.utils.TilesUtils;

public class ProcessTileInserts implements DocProcessor{

	private final static Logger LOG = LoggerFactory.getLogger(ProcessTileInserts.class);
	/**
	 * @param p
	 * @param pc
	 * @throws RESyntaxException
	 */
	/*public ProcessTileInserts(JSPPage p, String pc, JSPFileList fileList)
			throws RESyntaxException {
		RE re;
		int index;
		re = new RE("<tiles:insert[:space:]*definition=\"\\S*\"[:space:]>");
		index = 0;
		while (re.match(pc, index)) {
			// System.out.println(re.getParen(0));
			String link = re.getParen(0);
			link = ProcessorUtils.figureLink(link, p, JSPPage.TILE_INSERTS);
			JSPPage dest = ProcessorUtils.getPage(link, fileList);
			if (dest != null)
				dest.addConnection(JSPPage.TILE_INSERTS, p.getName());
			index = re.getParenEnd(0);
		}
	}*/
	
	/**
	 * @param p
	 * @param pc
	 * @param rootSourceDir 
	 * @throws RESyntaxException
	 */
	public ProcessTileInserts(JSPPage p, String pc, JSPFileList fileList, String rootSourceDir)
			throws RESyntaxException {		
		LOG.trace("Entering {}", "ProcessTileInserts");
		// Find page tag
		RE re;
		//Look for tile:inserts with definitions
		re = new RE("<tiles:insert[:space:]*definition=\"([:print:]*?)\"[:print:]*>");
		String[] matchRes = re.getAllMatches(pc, 1);		
		for(int results=0; results < matchRes.length; results++) {
		    re = new RE(",");
		    String[] inserts = re.split(matchRes[results]);		    
		    for(int i = 0; i < inserts.length; i++) {
		    	//Get the path from TilesUtils
		    	String[] link = TilesUtils.figureLink(inserts[i]);			    			    	
		    	if(link != null){
		    		for(String s : link){
			    		s = s.replaceAll("/", "\\\\");			    		
			    		JSPPage dest = ProcessorUtils.getPage(rootSourceDir+s, fileList);
			    		//There can be a scenario where the jsp page is not available. This is infact has to be fixed in app.
			    		//We need to handle it
			    		if(dest != null)
			    			dest.addTileInserts(inserts[i],  p.getName());
			    	}	
		    	}		    	
		    }
		}
		
		//Look for tile:inserts with attributes
		re = new RE("<tiles:insert[:space:]*attribute=\"([:print:]*?)\"[:print:]*>");
		matchRes = re.getAllMatches(pc, 1);		
		for(int results=0; results < matchRes.length; results++) {
		    re = new RE(",");
		    String[] inserts = re.split(matchRes[results]);		    
		    for(int i = 0; i < inserts.length; i++) {
		    	//Get the path from TilesUtils		    	
		    	String[] link = TilesUtils.figureLink(inserts[i]);		    	
		    	if(link != null){
		    		for(String s : link){
			    		s = s.replaceAll("/", "\\\\");			    		
			    		JSPPage dest = ProcessorUtils.getPage(rootSourceDir+s, fileList);
			    		//For tiles there can be tiles defs inserts as value for a put. 
			    		//We need to handle it diffrently so that it picks the value
			    		if(dest == null){
			    			//Obtain the value from TilesUtils
			    			String[] tilePaths = TilesUtils.figureLink(s);
			    			if(tilePaths != null){
			    				for(String tile : tilePaths){
				    				try{
				    					String links = ProcessorUtils.figureLink(tile, p, JSPPage.TILE_INSERTS);
				    				}catch (NullPointerException e) {
			    						LOG.error("Exception Occured and I am recovering from a NPE. {}", ExceptionUtils.getFullStackTrace(e));
			    					}		
				    			}
			    			}			    			
			    		}else{
			    			dest.addTileInserts(inserts[i],  p.getName());
			    		}
				    	
			    	}
		    	}		    			    
		    }
		}
		LOG.trace("Exiting {}", "ProcessTileInserts");
	}
	public boolean process(JSPPage p, String pc, JSPFileList fileList)
			throws RESyntaxException {
		// TODO Auto-generated method stub
		return false;
	}
	
}
